# core/analysis_module.py
from datetime import datetime


class StressAnalyzer:
    def __init__(self, part_doc):
        self.part = part_doc
        self.analysis = None

    def run_static_stress(self):
        """执行静态应力分析"""
        try:
            # 创建分析实例
            analysis_manager = self.part.AnalysisManager
            self.analysis = analysis_manager.AddStaticStressAnalysis()

            # 设置参数
            self.analysis.MeshSettings.AutomaticMesh = True
            self.analysis.Environment.Temperature = 293.15  # 20°C

            # 运行分析
            self.analysis.RunAnalysis()

            # 生成报告
            report_path = f"reports/stress_{datetime.now().strftime('%Y%m%d_%H%M')}.pdf"
            self.analysis.ExportReport(report_path)

            return report_path
        except Exception as e:
            raise RuntimeError(f"应力分析失败: {str(e)}")


# 使用示例
analyzer = StressAnalyzer(model)
report = analyzer.run_static_stress()
print(f"分析报告已生成: {report}")